__version__ = '0.0.1';

'use strict';

startup();

function append_html(html) {
    $("body").append(html);
};

function startup() {
    append_html(`
    <button id="connectButton">Connect</button>
    <button id="disconnectButton" disabled>Disconnect</button>

    <div id="text_exchange_div" style="max-width: 500px; background-color:#CC6699">

        <p>core node的信息:</p>

        <p id="self_text_str" style="background-color:#00FFFF">等待自身信息生成</p>

        <p>传入信息输入区:</p>
        <textarea id="input_text_textarea"></textarea>
        <button id="confirm_input_text_btn" disabled>确认传入</button>

    </div>
    
    `)
    $('#connectButton').click(connect);
    $('#disconnectButton').click(disconnect);
}

let core_node;
function connect(e) {
    $('#connectButton').attr("disabled",true);
    $('#disconnectButton').attr("disabled",false);

    core_node = new DataNode(name = 'core_node');

    core_node._send_one_key = (combined_key) => {
        $('#self_text_str').append(
            `
            <div id='${combined_key.target_id}_self_text_str'>
            <p>${combined_key.target_id}</p>
            <p style="background-color:#0066FF">${JSON.stringify(combined_key)}</p>
            </div>
            `
        )
    };

    core_node._clean_keys = () => {
        $('#self_text_str').empty();
    }

    core_node.onopen = (e) => {

    };
    core_node.onclose = (e) => {

    };
    // core_node.DataRecivers = {};
    core_node.onMessage = (event) => {
        // for (name in DataRecivers){
        //     core_pc.DataRecivers[name](event.data);
        // }
    }
}

function disconnect() {
    // Close the RTCPeerConnections
    core_node.close();
    core_node = null;
    $('#self_text_str').text('等待自身信息生成');
    $('#connectButton').attr("disabled",false);
    $('#disconnectButton').attr("disabled",true);
}
